package com.wrox;

import java.sql.SQLException;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DataSourceLocator {
	
	private static DataSourceLocator dsl = null;
	private  DataSource ds = null;
	
	private DataSourceLocator(){
		
	}
	
	public static DataSourceLocator getDataSourceLocator()
	{
		if(dsl==null)
		{
			dsl = new DataSourceLocator();
		}
		return dsl;
	}
	
	public  javax.sql.DataSource getDataSource(String dataSourceName) throws SQLException
	{
		 	Context ctx = null;
		 	Hashtable ht = new Hashtable();
		 	ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
		    ht.put(Context.PROVIDER_URL,"t3://localhost:7001");

		  try {
		    ctx = new InitialContext(ht);
		    ds = (javax.sql.DataSource) ctx.lookup(dataSourceName);
		    System.out.println(ds.getLoginTimeout());

		  }
		  catch (NamingException e) {
		    e.printStackTrace();
		  }
		  finally {
		    try {ctx.close();}
		 catch (Exception e) {
		      // a failure occurred
		    }
		  }
		  return ds;

	}

}
